/**
 * 
 */
package com.application.Activity;

import it.unipr.ce.dsg.s2p.sip.Address;

import java.util.ArrayList;

import org.w3c.dom.Document;

import ReadWriteXML.CMember;
import ReadWriteXML.CNeighbor;
import ReadWriteXML.ReadXML;
import ReadWriteXML.WriteXML;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.application.Adapter.MyInviteChatAdapter;
import com.application.Controller.ActivityController;
import com.application.Controller.NetworkController;
import com.application.Controller.ReadWriteFileController;
/**
 * Class invite chat group
 */
public class FrmInviteGroupChat extends Activity {
	static MyInviteChatAdapter _myAdapter;
	static ListView _lvNeighbor;
	EditText _txtTitleInvite;
	ImageView _btnInvite ;
	CheckBox _ckinvite;
	private static ArrayList<CNeighbor> _listNeighbor;
	static Document _doc;
	static Context _cont;
	static boolean _first = true;
	Document _docInvite = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.frm_invitechat);
		//ThreadConnectToUser.init_runnableUpdateUserList();
		NetworkController.init_runnableUpdateUserList();
		_cont = this;
		_btnInvite = (ImageView)findViewById(R.id.invitechat_imginvite);
		_ckinvite=(CheckBox) findViewById(R.id.rowinvite_ckinvite);
		_txtTitleInvite = (EditText) findViewById(R.id.invitechat_edtmessageinvite);
		_lvNeighbor = (ListView) findViewById(R.id.invitechat_lvlistuser);
		setAdapter();

		_btnInvite.setOnClickListener(onInviteListener);
	}

	//---------su kien click vao nut moi nhom-----------------
	OnClickListener onInviteListener= new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			NetworkController.set_isTeamLeader(true);
			Log.d("PEERDROID", "NetworkController.get_ipUser() invite: " + NetworkController.is_isTeamLeader());
			//--------khoi tao 1 file xml de ghi nhom do minh tao
			Toast toast = Toast.makeText(getBaseContext(), " ham moi vao group", Toast.LENGTH_SHORT);
			toast.show();
			String myUserName=NetworkController.get_userName();
			String path = ReadWriteFileController.DIRECTORYCHATGROUP + "Group_"+myUserName+ "_ipList.xml";
			String title = _txtTitleInvite.getText().toString();
			//nếu file chưa tồn tại thì tạo file mới
			if(!ReadWriteFileController.isFile(path))
			{
				_docInvite = WriteXML.insertRoot(_docInvite, path, "ListLearner");
				Log.d("PEERDROID", "NetworkController.get_ipUser(): " + NetworkController.get_ipUser());
				CMember mem = new CMember(NetworkController.get_ipUser(), NetworkController.get_fullName(), NetworkController.get_userName());
				_docInvite = WriteXML.insertMember(_docInvite, path, mem);
			}
			for (int i = 0; i < _listNeighbor.size(); i++) {
				//------gui thong tin moi vao nhom--------
				if(_listNeighbor.get(i).get_isinvite()==true){
					//gui thong diep moi vao nhom
					String s = _listNeighbor.get(i).get_member().get_ip().toString();
					NetworkController.SendInvite(new Address(s), title);
					Log.d("PEERDROID", "IP dc moi la: " + s);
				}
			}
		}
	};

	private void setChangeCheck(){
		if (_ckinvite.isChecked()) {
			_ckinvite.setChecked(false);
		}
	}
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.d("PEERDROID","vao onstart user list");
		ActivityController.setPeerActivity(this);
	}
	private static ArrayList<CNeighbor> readNeighbor() {
		ArrayList<CNeighbor> listNeighbor = new ArrayList<CNeighbor>();
		ArrayList<CMember> listMember = new ArrayList<CMember>();
		_doc = ReadXML.BuildXML(ReadWriteFileController.pathNeighborsList);
		Log.d("PEERDROID","pathNeighborsList: " + ReadWriteFileController.pathNeighborsList);
		listMember = ReadXML.ReadMember(_doc);
		boolean temp=false;

		for(int i=0;i<listMember.size();i++){
			CMember mem = new CMember();
			mem=listMember.get(i);
			CNeighbor neighbor=new CNeighbor(mem, temp);
			listNeighbor.add(neighbor);
		}
		return listNeighbor;

	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)  {
		Log.d("PEERDROID", " vao on key down");
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setIcon(R.drawable.img_dialog);
			builder.setMessage("Bạn có chắc muốn thoát khỏi chương trình ?")
			.setCancelable(false)
			.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					if(!NetworkController.isOffline())
					{
						String s = NetworkController.get_ipHost();
						NetworkController.SendMessageLogout();
//						NetworkController.peerRelease();
						//xoá danh sách hàng xóm khi thoát khỏi ứng dụng
//						ReadWriteFileController.deleteFile(ReadWriteFileController.pathNeighborsList);
					}
					//NetworkController.SendMessageLogout(new Address("192.168.173.20:5033"));
					Log.d("PEERDROID","Đã Thoát khỏi chương trình");
					finish();
//					System.exit(0);
				}
			})
			.setNegativeButton("No", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();
				}
			});
			AlertDialog alert = builder.create();
			alert.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 255, 255, 255))); 
			alert.show();
			return true;
		}
		return super.onKeyDown(keyCode, event);	
	}
	public static  void setAdapter() {
		Log.d("PEERDROID","set Adapter user list");
		_listNeighbor = readNeighbor();
		Log.d("PEERDROID","_cont:" + _cont);
		Log.d("PEERDROID","_listNeighbor" + _listNeighbor.size());
		Log.d("PEERDROID","set Adapter user list");
		_myAdapter = new MyInviteChatAdapter(_cont, R.layout.listrow_invitechat,_listNeighbor);
		_lvNeighbor.setAdapter(_myAdapter);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//		ShowSubjectList();	
		Log.d("PEERDROID", "vao resume");
		ActivityController.setPeerActivity(this);
	}
}
